# -*- coding: utf-8 -*-
class AdvancePaymentController < ApplicationController
  before_filter :employee_authorize
  before_filter :sys_before_log
  before_filter :employee_action_hx
  
  def advanceList

  end

  def newAdvance

  end

  def editAdvance

  end

  def create
    begin
      advancePayment = AdvancePayment.new()
      advancePayment.company_id = session[:company_id]
      advancePayment.department_id = session[:department_id]
      advancePayment.employee_id = session[:employee_id]
      advancePayment.customer_id = params[:customer_id]
      advancePayment.action_date = params[:action_date]
      advancePayment.bank_id = params[:bank_id]
      advancePayment.account = params[:account]
      advancePayment.amount = params[:amount]
      advancePayment.promotion_id = params[:promotion_id]
      advancePayment.description = params[:description]
      advancePayment.save!
      info = "成功"
    rescue Exception => e
      info = e
      DbLog.info(session[:company_id], session[:department_id], session[:employee_id], params[:controller], params[:action], request.remote_ip ,e.to_s)
    end
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end

  def update
    begin
      advancePayment = AdvancePayment.find(params[:id])
      advancePayment.employee_id = session[:employee_id]
      advancePayment.customer_id = params[:customer_id]
      advancePayment.action_date = params[:action_date]
      advancePayment.bank_id = params[:bank_id]
      advancePayment.account = params[:account]
      advancePayment.amount = params[:amount]
      advancePayment.promotion_id = params[:promotion_id]
      advancePayment.description = params[:description]
      advancePayment.save!
      info = "成功"
    rescue Exception => e
      info = e.to_s
      DbLog.info(session[:company_id], session[:department_id], session[:employee_id], params[:controller], params[:action], request.remote_ip ,e.to_s) end
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end

  def getAllAdvancePayments
    advancePayments = AdvancePayment.all
    jsonStr="{totalProperty:"+advancePayments.length.to_s+",root:"+advancePayments.to_json(:include=>[:employee,:promotion,:customer])+"}"
    #logger.info jsonStr
    render :text=>jsonStr, :layout=>false
  end

  def getAdvancePaymentById
    advancePayment = AdvancePayment.find(params[:id])
    rootStr = advancePayment.to_json()
    jsonStr = "{root:["+rootStr+"]}"
    #logger.info jsonStr
    render :text=>jsonStr, :layout=>false
  end
end
